package com.market.sys.api.client;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.market.sys.common.Result;
import com.market.sys.entity.Article;
import com.market.sys.service.IArticleService;
import com.market.sys.service.IUserCollectionService;
import com.market.sys.service.IUserLikeService;
import com.market.sys.service.IUserService;
import com.market.sys.util.EmptyUtil;
import com.market.sys.util.TimeUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;

    @Resource
    private IUserLikeService userLikeService;

    @Resource
    private IUserCollectionService userCollectionService;

    @PostMapping("/fetchListByCondition")
    public Result<?> fetchListByCondition(@RequestBody Map<String,Object> conditionMap){
        return articleService.findByCondition(conditionMap);
    }

    @PostMapping("/fetchCollectList")
    public Result<?> fetchCollectList(@RequestBody Map<String,Object> conditionMap){
        return articleService.fetchCollectList(conditionMap);
    }

    @PostMapping("/fetchLikeList")
    public Result<?> fetchLikeList(@RequestBody Map<String,Object> conditionMap){
        return articleService.fetchLikeList(conditionMap);
    }

    @PostMapping("/fetchArticleDetail")
    public Result<?> fetchArticleDetail(@RequestBody Map<String, Object> paramMap){
        Article article = articleService.query().eq("id", String.valueOf(paramMap.get("id"))).one();
        article.setPublishTime(TimeUtil.completeGetTimeStamp(article.getPublishTime()));
        Long likeCount = userLikeService.query().eq("thing_id", String.valueOf(paramMap.get("id"))).eq("type", 1).count();
        Long collectCount = userCollectionService.query().eq("thing_id",String.valueOf(paramMap.get("id"))).eq("type",1).count();

        int articleCount = Integer.parseInt(article.getViewCount());
        articleCount = articleCount + 1;
        articleService.update().set("view_count",articleCount).eq("id",article.getId()).update();

        Map<String, Object> mapData = new HashMap<>(3);
        mapData.put("article",article);
        mapData.put("likeCount", likeCount);
        mapData.put("collectCount", collectCount);
        return Result.success(mapData);
    }
}